//
//  SSHomeRetweetView.swift
//  SSSina
//
//  Created by ss on 2017/7/4.
//  Copyright © 2017年 ss. All rights reserved.
//

import UIKit
import SnapKit
class SSHomeRetweetView: UIView {
    
    // 记录转发微博的底部约束
    var retweetViewBottomConstraint: Constraint?
    
    // 提供属性供外界赋值
    var statusViewModel: SSStatusViewModel?{
        didSet{
            // 赋值
            contentLabel.text = statusViewModel?.homeModel?.retweeted_status?.text
            
            /*
             - 卸载约束
             - 判断是否有配图
             - 判断条件: statusViewModel?.homeModel?.retweeted_status?.pic_urls
             - 如果pic_urls.count > 0
             - 有配图
             - 赋值
             - retweetView.bottom = pictureView.bottom + 10
             - 显示配图
             - 反之没有配图
             - retweetView.bottom = contentLabel.bottom + 10
             - 隐藏配图
             
             */
            
            // 卸载约束
            retweetViewBottomConstraint?.deactivate()
            
            // 有配图
            if let pic_urls = statusViewModel?.homeModel?.retweeted_status?.pic_urls, pic_urls.count > 0 {
                // 赋值
                pictureView.picUrlsList = pic_urls
                snp.makeConstraints({ (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                pictureView.isHidden = false
            }else{
                // 没有配图
                snp.makeConstraints({ (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                
                pictureView.isHidden = true
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RGB(r: 235, g: 235, b: 235)
        // 添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        // 添加约束
        contentLabel.snp.makeConstraints { (make) in
            make.top.left.equalTo(10)
        }
        
        pictureView.snp.makeConstraints { (make) in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(10)
        }
        
        // 转发微博的底部约束
        snp.makeConstraints { (make) in
            retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
    }
    
    
    //MARK: - 懒加载控件
    // 微博正文
    private lazy var contentLabel: UILabel = UILabel(text: "", textColor: UIColor.black, fontSize: SSNormalFontSize, maxWidth: SSSCREENW - 20)
    // 配图
    private lazy var pictureView: SSHomePictureView = {
        let view = SSHomePictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()}
